<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "config/config.php";
include "auth.php";
include "DBManager.php";

// -----------------------------------------------------------------------------
// 访问设置
// -----------------------------------------------------------------------------

$requiredVar = [
    "navPageShowMostOftenClicked",
    "navPageShowRecentlyClicked",
    "navPageShowBadges",
    "navPageOpenLinkInNewTab",
    "linkSortOrder",
    "maxNumberOfLinksShowInRow",
    "miniNumberOfLinksShowInRow",
];

foreach ($requiredVar as $var) {
    if (!isset($_POST[$var])) {
        exit("需要传入有效的参数！");
    }
}

// -----------------------------------------------------------------------------
// 偏好设定相关
// -----------------------------------------------------------------------------

$navPageShowMostOftenClicked = $_POST["navPageShowMostOftenClicked"];
$navPageShowRecentlyClicked = $_POST["navPageShowRecentlyClicked"];
$navPageShowBadges = $_POST["navPageShowBadges"];
$navPageOpenLinkInNewTab = $_POST["navPageOpenLinkInNewTab"];
$linkSortOrder = $_POST["linkSortOrder"];
$maxNumberOfLinksShowInRow = $_POST["maxNumberOfLinksShowInRow"];
$miniNumberOfLinksShowInRow = $_POST["miniNumberOfLinksShowInRow"];

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------
// 函数定义
// -----------------------------------------------------------------------------

function linkGenerator($linkId, $linkTitle, $linkUrl, $timesOfClick, $navPageShowBadges, $navPageOpenLinkInNewTab) {

    $badges = "";
    if ($navPageShowBadges == "true") {
        $badges = " " . <<<BBB
            <span class="badge rounded-pill bg-secondary" style="font-size: 0.5rem">$timesOfClick</span>
        BBB;
    }

    $target = "";
    if ($navPageOpenLinkInNewTab == "true") {
        $target = "_blank";
    } else {
        $target = "_self";
    }

    return <<<BBB
        <a href="$linkUrl" class="col text-decoration-none">
            <span data-id="$linkId" 
                 data-title="$linkTitle"
                 data-url="$linkUrl" 
                 data-target="$target"
                 class="card h-100 btnClickLink">
                <span class="card-body p-2">
                    <h5 class="card-title mb-0 text-center text-dark">{$linkTitle}{$badges}</h5>
                </span>
            </span>
        </a>
    BBB;

}

// -----------------------------------------------------------------------------

$categories = $db->getListOfCategories();

// -----------------------------------------------------------------------------
?>
<!-- 隐藏的元素，以在小屏幕上显示分类列表 开始-->
<nav id="sidebarOnPhone" class="bg-light position-fixed sidebar sidebarOnPhone" style="z-index: 1000; width: 100%; display: none">
    <!-- <div class="bg-secondary p-4">
        <p class="text-white mt-1 mb-1">
            <a href="#" class="btnLogout text-white text-decoration-none" target="_self">退出登录</a>
        </p>
    </div> -->
    <ul class="nav flex-column mt-3" style="margin-bottom: 120px">
        <?php

        if ($navPageShowMostOftenClicked == "true") {
            echo <<<BBB
                <li class="nav-item">
                    <a href="#categoryIdMostOftenClicked" 
                       class="nav-link d-flex align-items-center active jumpToCategoryFromPhone py-1 text-break text-danger">最常访问</a>
                </li>
            BBB;
        }

        if ($navPageShowRecentlyClicked == "true") {
            echo <<<BBB
                <li class="nav-item">
                    <a href="#categoryIdRecentlyClicked" 
                       class="nav-link d-flex align-items-center active jumpToCategoryFromPhone py-1 text-break text-danger">最近访问</a>
                </li>
            BBB;
        }

        foreach ($categories as $category) {

            $tempCategoryId = $category["id"];
            $tempCategoryTitle = $category["title"];

            $numberOfLinks = $db->getNumberOfLinksInCategory($tempCategoryId);
            // 网址数量为0的分类就不显示了。
            if ($numberOfLinks > 0) {
                echo <<<BBB
                    <li class="nav-item">
                        <a href="#categoryId{$tempCategoryId}" 
                           class="nav-link d-flex align-items-center active jumpToCategoryFromPhone py-1 text-break">$tempCategoryTitle</a>
                    </li>
                BBB;
            }

        }

        ?>
    </ul>
</nav>
<!-- 隐藏的元素，以在小屏幕上显示分类列表 结束-->

<div class="container-fluid mt-5">

    <div class="row">

        <!--左侧分类列表 开始-->
        <nav id="sidebar" class="col-md-3 col-lg-2 bg-light position-fixed sidebar d-none d-sm-none d-md-block collapse" style="z-index: 1000">
            <ul class="nav flex-column mt-3">
                <?php

                if ($navPageShowMostOftenClicked == "true") {
                    echo <<<BBB
                        <li class="nav-item">
                            <span data-href="#categoryIdMostOftenClicked" 
                                    role="button"
                                    class="nav-link d-flex align-items-center active jumpToCategory py-1 text-break text-danger">最常访问</span>
                        </li>
                    BBB;
                }

                if ($navPageShowRecentlyClicked == "true") {
                    echo <<<BBB
                        <li class="nav-item">
                            <span data-href="#categoryIdRecentlyClicked" 
                                    role="button"
                                    class="nav-link d-flex align-items-center active jumpToCategory py-1 text-break text-danger">最近访问</span>
                        </li>
                    BBB;
                }

                foreach ($categories as $category) {

                    $tempCategoryId = $category["id"];
                    $tempCategoryTitle = $category["title"];

                    $numberOfLinks = $db->getNumberOfLinksInCategory($tempCategoryId);
                    // 网址数量为0的分类就不显示了。
                    if ($numberOfLinks > 0) {
                        echo <<<BBB
                            <li class="nav-item">
                                <span data-href="#categoryId{$tempCategoryId}" 
                                      role="button"
                                      class="nav-link d-flex align-items-center active jumpToCategory py-1 text-break">$tempCategoryTitle</span>
                            </li>
                        BBB;
                    }

                }

                echo <<<BBB
                    <span class="mt-3"></span>
                BBB;

                ?>
            </ul>

        </nav>
        <!--左侧分类列表 结束-->

        <!--右侧网址列表 开始-->
        <main class="col-md-9 d-block ms-sm-auto col-lg-10 px-3 mt-4">

            <!-- 导航栏 开始 -->
            <div class="">
                <nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
                    <span class="d-flex">
                        <ol class="breadcrumb w-100">
                            <li class="breadcrumb-item"><a class="text-decoration-none" href="<?php echo $homepage; ?>">首页</a></li>
                            <li class="breadcrumb-item active">网址导航</li>
                        </ol>
                        <span class="flex-shrink-0">
                            <select class="form-control form-select form-select-sm selectLinkSortOrder" name="linkSortOrder" id="selectLinkSortOrder">
                                <option value="" style="color: darkgray" disabled>-- 排序方式 --</option>
                                <option value="1" <?php if ($linkSortOrder == "1") {echo "selected";} ?> >按排序号排列</option>
                                <option value="2" <?php if ($linkSortOrder == "2") {echo "selected";} ?> >点击数多靠前</option>
                                <option value="3" <?php if ($linkSortOrder == "3") {echo "selected";} ?> >最近添加靠前</option>
                                <option value="4" <?php if ($linkSortOrder == "4") {echo "selected";} ?> >最近编辑靠前</option>
                                <option value="5" <?php if ($linkSortOrder == "5") {echo "selected";} ?> >最近移动靠前</option>
                                <option value="6" <?php if ($linkSortOrder == "6") {echo "selected";} ?> >最近访问靠前</option>
                            </select>
                        </span>
                    </span>
                </nav>
            </div>
            <!-- 导航栏 结束 -->

            <!--最常访问 开始-->
            <?php
            if ($navPageShowMostOftenClicked == "true") {
                $linksMostOftenClicked = $db->getListOfLinksMostOftenClicked(12);
                if (count($linksMostOftenClicked) > 0) {

                    echo <<<BBB
                        <div class="mb-3">
                            <h4 id="categoryIdMostOftenClicked"
                                class="pb-2 text-danger border-bottom border-danger text-break">最常访问</h4>
                            <!-- <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3"> -->
                            <div class="row row-cols-{$miniNumberOfLinksShowInRow} row-cols-sm-2 row-cols-md-2 row-cols-xl-{$maxNumberOfLinksShowInRow} py-2 g-2">
                    BBB;

                    foreach ($linksMostOftenClicked as $link) {
                        $tempLinkId = $link["id"];
                        $tempLinkUrl = $link["url"];
                        $tempLinkTitle = $link["title"];
                        $timesOfClick = $link["timesOfClick"];
                        echo linkGenerator($tempLinkId, $tempLinkTitle, $tempLinkUrl, $timesOfClick, $navPageShowBadges, $navPageOpenLinkInNewTab);
                    }

                    echo <<<BBB
                            </div>
                        </div>
                    BBB;

                }
            }
            ?>
            <!--最常访问 结束-->

            <!--最近访问 开始-->
            <?php
            if ($navPageShowRecentlyClicked == "true") {
                $linksRecentlyClicked = $db->getListOfLinksRecentlyClicked(12);
                if (count($linksRecentlyClicked) > 0) {

                    echo <<<BBB
                        <div class="mb-3">
                            <h4 id="categoryIdRecentlyClicked"
                                class="pb-2 text-danger border-bottom border-danger text-break">最近访问</h4>
                            <!-- <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3"> -->
                            <div class="row row-cols-{$miniNumberOfLinksShowInRow} row-cols-sm-2 row-cols-md-2 row-cols-xl-{$maxNumberOfLinksShowInRow} py-2 g-2">
                    BBB;

                    foreach ($linksRecentlyClicked as $link) {
                        $tempLinkId = $link["id"];
                        $tempLinkUrl = $link["url"];
                        $tempLinkTitle = $link["title"];
                        $timesOfClick = $link["timesOfClick"];
                        echo linkGenerator($tempLinkId, $tempLinkTitle, $tempLinkUrl, $timesOfClick, $navPageShowBadges, $navPageOpenLinkInNewTab);
                    }

                    echo <<<BBB
                            </div>
                        </div>
                    BBB;

                }
            }
            ?>
            <!--最近访问 结束-->

            <!--自定义网址导航 开始-->
            <?php
            $totalNumberOfLinks = $db->gettotalNumberOfLinks();
            if (count($categories) == 0 || $totalNumberOfLinks == 0) {

                echo <<<BBB
                    <div class="alert alert-warning mb-0" role="alert">
                        <p class='text-muted mb-0'>暂无可用的数据，请先在首页的「<a href="category/index.php" class="text-decoration-none">分类管理</a>」中添加「分类」和「网址」。</p>
                    </div>
                BBB;

            } else {
                foreach ($categories as $category) {

                    $tempCategoryId = $category["id"];
                    $tempCategoryTitle = $category["title"];

                    $numberOfLinks = $db->getNumberOfLinksInCategory($tempCategoryId);

                    // 网址数量为0的分类就不显示了。
                    if ($numberOfLinks > 0) {

                        $totalTimesOfClick = $db->getTotalTimesOfClickOfCategory($tempCategoryId);

                        $badges = "";
                        if ($navPageShowBadges == "true") {
                            $badges = <<<BBB
                                <span class="badge rounded-pill bg-primary" style="font-size: 10px">{$numberOfLinks}网址</span>
                                <span class="badge rounded-pill bg-primary" style="font-size: 10px">{$totalTimesOfClick}点击</span> 
                            BBB;
                        }

                        echo <<<BBB
                            <div class="mb-3">
                                <h4 id="categoryId{$tempCategoryId}" 
                                    role="button"
                                    class="pb-2 text-primary border-bottom border-primary d-flex categoryTitle">
                                    <span class="w-100 hintMoveToTop text-break">$tempCategoryTitle <i class="bi bi-pin-angle" style="display: none; font-size: 20px"></i></span>
                                    <span class="flex-shrink-0">$badges</span>
                                    <span class="flex-shrink-0 mx-2">
                                        <a href="category/linksInCategory.php?categoryId={$tempCategoryId}">
                                            <i class="bi bi-pencil-square editLinksInCategory" style="font-size: 15px"></i>
                                        </a>
                                    </span>
                                </h4>

                                <!-- <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3"> -->
                                <div class="row row-cols-{$miniNumberOfLinksShowInRow} row-cols-sm-2 row-cols-md-2 row-cols-xl-{$maxNumberOfLinksShowInRow} py-2 g-2">
                        BBB;

                        $orders = [
                            "1" => "sortNumber",
                            "2" => "timesOfClick",
                            "3" => "timeCreate",
                            "4" => "timeLastEdit",
                            "5" => "timeLastMove",
                            "6" => "timeLastClick",
                        ];
                        $tempSortOrder = $orders[$linkSortOrder];
                        $linksInCategory = $db->getListOfLinksInCategory($tempCategoryId, $tempSortOrder);

                        foreach ($linksInCategory as $link) {
                            $tempLinkId = $link["id"];
                            $tempLinkTitle = $link["title"];
                            $tempLinkUrl = $link["url"];
                            $timesOfClick = $link["timesOfClick"];
                            echo linkGenerator($tempLinkId, $tempLinkTitle, $tempLinkUrl, $timesOfClick, $navPageShowBadges, $navPageOpenLinkInNewTab);
                        }

                        echo <<<BBB
                                </div>
                            </div>
                        BBB;

                    }

                }
            }

            ?>
            <!--自定义网址导航 结束-->

        </main>
        <!--右侧网址列表 结束-->

    </div>

</div>

<!--添加这片空白元素，主要是为了方便页内锚点跳转-->
<!--可以考虑替换成大型的返回顶部按钮，或者一些图片什么的-->
<div role="button" id="footer" class="mt-3 bg-secondary footer" style="height: 800px"></div>
